package com.example.administrator.mail_list;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;

public class StuAdapter extends RecyclerView.Adapter<StuViewHolder> {
    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnDelListener listener;
    private ClickViewListener clickViewListener;

    public StuAdapter(Context context,ArrayList<Student> studentArrayList,
                      IOnDelListener listener,ClickViewListener clickViewListener){
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener = listener;
        this.clickViewListener = clickViewListener;
    }

    //判断是哪个数组
    public void changeData(ArrayList<Student> studentArrayList){
        this.studentArrayList = studentArrayList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public StuViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //创建view
        View view = LayoutInflater.from(context)
                .inflate(R.layout.student_list_item,viewGroup,false);

        StuViewHolder viewHolder = new StuViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final StuViewHolder holder, final int i) {
        final Student s = studentArrayList.get(i);
        holder.numView.setText(s.getNumber());
        holder.imgView.setImageResource(s.getImgId());
        holder.nameView.setText(s.getName());
        holder.ageView.setText(String.valueOf(s.getAge()));

        //删除
        holder.delStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(s.getNumber());
            }
        });

        //recyclerView的点击事件
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //将view这个参数传递到MainActivity中，可通过接口实现
                clickViewListener.click(holder.view,i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }
}
